import time

from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from LoginTest import Login
from selenium.webdriver.common.by import By



class TemplatePage:
    url=""
    driver=""

    def __init__(self):
        self.url="http://49.235.61.184:8080/template"
        self.driver=blog_driver.driver
        self.form = FormHelper(self.driver)
        # 公共自动登录方法，未登录时执行Login().LoginSuTest
        #self.form.auto_login(self.url, login_func=lambda: Login().LoginSuTest())
        self.driver.get(self.url)

    #添加模板
    def AddTemplate(self):
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button")
        blog_driver.GetScreeShot()
        #添加
        self.form.fill_fields({"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div.quill.title > pre > div.ql-editor.ql-blank > p":"模板测试"})
        blog_driver.GetScreeShot()
        #保存
        self.form.click("#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button:nth-child(5)")
        #跳出提示
        #新增模板名称
        self.driver.find_element(By.XPATH,"//*[@id=\"name\"]").send_keys("测试添加模板")
        #从下拉菜单中选择
        self.driver.find_element(By.XPATH,"//*[@id=\"category\"]").send_keys("测试")
        blog_driver.GetScreeShot()
        #添加到公共库
        self.form.click("#survey-modal-container > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-body > form > div:nth-child(4) > div > div > div > div > label > span.ant-checkbox > input")
        #保存
        self.driver.find_element(By.CSS_SELECTOR,"#survey-modal-container > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.ant-btn-primary").click()
        blog_driver.GetScreeShot()
        time.sleep(5)
        #关闭
        self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm.ant-btn-dangerous").click()
        #检测是否添加成功
        self.form.verify_error_message("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.cardList > div > div > div > div > div:nth-child(1) > div > div > div > div.ant-card-body > div.ant-card-meta > div > div.ant-card-meta-title > a","测试添加模板")

    #模板名称搜索
    def SearchTemplateTest(self):
        self.form.fill_fields({"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > input":"测试添加模板"})

        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > span > button")
        blog_driver.GetScreeShot()
        actual = self.driver.find_element(By.XPATH, "//*[contains(text(),'暂无数据')]").text
        print(f"找到的文本内容: {actual}")
        # 断言检测一下是否符合预期
        assert "暂无数据" in actual

# person=TemplatePage()
# person.AddTemplate()
# person.SearchTemplateTest()
